package com.weixing.mall.core.support;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weixing.mall.base.constant.GlobalConstant;
import com.weixing.mall.base.orm.MPage;
import com.weixing.mall.core.support.auth.AuthDto;
import com.weixing.mall.util.ThreadLocalMap;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: kfc
 * @Description: <br/>
 * Date:Create in 2019/5/14 19:34
 * @Modified By:
 */
public class BaseController {

    @Autowired
    protected HttpServletRequest req;

    protected Page initMpPage(){
        String cur = req.getParameter("currentPage");
        String size = req.getParameter("pageSize");
        int curPage = (cur == null || "0".equals(cur) ? 1 : Integer.parseInt(cur));
        int pageSize = (size == null ? GlobalConstant.DEFAULT_PAGE_SIZE : Integer.parseInt(size));
        return new Page(curPage, pageSize);
    }

    protected MPage initMPage(){
        String cur = req.getParameter("currentPage");
        String size = req.getParameter("pageSize");
        int curPage = (cur == null || "0".equals(cur) ? 1 : Integer.parseInt(cur));
        int pageSize = (size == null ? GlobalConstant.DEFAULT_PAGE_SIZE : Integer.parseInt(size));
        return new MPage(curPage, pageSize);
    }


    /**
     * 获取当前访问用户的id
     *
     * @return
     */
    protected  long getCurrentUserId(){
        String uid = req.getHeader(GlobalConstant.X_HEADER_UID);
        return StringUtil.isBlank(uid) ? -1 : Long.parseLong(uid);
    }


    protected AuthDto getUserAuthDto(){
        return (AuthDto) ThreadLocalMap.get(GlobalConstant.TLMK_USER_AUTH_DTO);
    }
}
